** Put together information on the number of kids and age of youngest kid 
** for each mother and father, for use in intergenerational EITC project

**********************************************************************************



**************
** 1990
**************



** Wave 3

quietly infile using sip90w3.dct, using(s90w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)



drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1990_w3, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1990_w3, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip90w3.dct, using(s90w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1990_w3
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1990_w3, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1990_w3, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1990_w3, replace
clear




** Wave 4

quietly infile using sip90w4.dct, using(s90w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)



drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1990_w4, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1990_w4, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip90w4.dct, using(s90w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1990_w4
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1990_w4, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1990_w4, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1990_w4, replace
clear





**************
** 1991
**************



** Wave 3

quietly infile using sip91w3.dct, using(sipp91w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)



drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1991_w3, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1991_w3, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip91w3.dct, using(sipp91w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1991_w3
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1991_w3, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1991_w3, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1991_w3, replace
clear




** Wave 7

quietly infile using sip91w7.dct, using(sipp91w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)



drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1991_w7, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1991_w7, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip91w7.dct, using(sipp91w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1991_w7
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1991_w7, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1991_w7, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1991_w7, replace
clear




**************
** 1992
**************



** Wave 4


quietly infile using sip92w4.dct, using(sipp92w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)



** What is up with suid 801123756?
**browse if suid=="801123756"
** This special case illustrates possibly broader problem that a kid and her 
** parent might not have the same entry.  So when you later treat parent_pnum
** as pnum, that is causing a very small number of cases where suid entry pnum 
** don't uniquely identify an observation.  Deal with this one in a hand-crafted
** way.
gen entry2 = entry
replace entry2 = "31" if addid=="31" & suid=="801123756"
replace entry2 = "32" if addid=="32" & suid=="801123756"

drop entry
rename entry2 entry

drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1992_w4, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1992_w4, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip92w4.dct, using(sipp92w4.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1992_w4
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1992_w4, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1992_w4, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1992_w4, replace
clear




** 1992, Wave 6


quietly infile using sip92w6.dct, using(sipp92w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid addid entry pnum swave srefmon tage pnpt enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid srefmon parent_pnum srefmon

by suid addid srefmon parent_pnum : egen KidCount = total(HasParent)
by suid addid srefmon parent_pnum : egen klt24m = total(under24FTS_HasParent)
by suid addid srefmon parent_pnum : egen klt19m = total(under19_HasParent)
by suid addid srefmon parent_pnum : egen klt18m = total(under18_HasParent)
by suid addid srefmon parent_pnum : egen klt6m = total(under6_HasParent)
by suid addid srefmon parent_pnum : egen klt5m = total(under5_HasParent)
by suid addid srefmon parent_pnum : egen DepCount = total(PotentialDep)




drop if parent_pnum==.

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
duplicates report suid addid pnum swave srefmon
save KidCount1992_w6, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1992_w6, replace
clear




************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip92w6.dct, using(sipp92w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1992_w6
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid entry pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1992_w6, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid entry pnum swave srefmon
save KidCount1992_w6, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid addid pnum swave srefmon
save KidCount1992_w6, replace
clear





**************
** 1993
**************

** Wave 3


quietly infile using sip93w3.dct, using(sipp93w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid entry pnum swave srefmon tage pnpt addid enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))


gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid parent_pnum srefmon

by suid addid parent_pnum srefmon: egen KidCount = total(HasParent)
by suid addid parent_pnum srefmon: egen klt24m = total(under24FTS_HasParent)
by suid addid parent_pnum srefmon: egen klt19m = total(under19_HasParent)
by suid addid parent_pnum srefmon: egen klt18m = total(under18_HasParent)
by suid addid parent_pnum srefmon: egen klt6m = total(under6_HasParent)
by suid addid parent_pnum srefmon: egen klt5m = total(under5_HasParent)
by suid addid parent_pnum srefmon: egen DepCount = total(PotentialDep)

drop if parent_pnum==.
keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
save KidCount1993_w3, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1993_w3, replace
clear



************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip93w3.dct, using(sipp93w3.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1993_w3
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid addid pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1993_w3, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid addid pnum swave srefmon
save KidCount1993_w3, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount =rowtotal(DepCount sp_DepCount)

sort suid entry pnum swave srefmon
save KidCount1993_w3, replace
clear



****************
** 1993, Wave 6
****************

quietly infile using sip93w6.dct, using(sipp93w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid entry pnum swave srefmon tage pnpt addid enrold ms

gen under24FTS=(tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))

gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)


sort suid addid parent_pnum srefmon

by suid addid parent_pnum srefmon: egen KidCount = total(HasParent)
by suid addid parent_pnum srefmon: egen klt24m = total(under24FTS_HasParent)
by suid addid parent_pnum srefmon: egen klt19m = total(under19_HasParent)
by suid addid parent_pnum srefmon: egen klt18m = total(under18_HasParent)
by suid addid parent_pnum srefmon: egen klt6m = total(under6_HasParent)
by suid addid parent_pnum srefmon: egen klt5m = total(under5_HasParent)
by suid addid parent_pnum srefmon: egen DepCount = total(PotentialDep)

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if parent_pnum==.
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
save KidCount1993_w6, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1993_w6, replace
clear



************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip93w6.dct, using(sipp93w6.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1993_w6
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid addid pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1993_w6, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid addid pnum swave srefmon
save KidCount1993_w6, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(DepCount sp_DepCount)

sort suid entry pnum swave srefmon
save KidCount1993_w6, replace
clear





****************
** 1993, Wave 7
****************

quietly infile using sip93w7.dct, using(sipp93w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon
rename age tage

keep suid entry pnum swave srefmon tage pnpt addid enrold ms

gen under24FTS = (tage<24 & enrold==1 & (ms~=1 & ms~=2))
gen under19=(tage<19 & (ms~=1 & ms~=2))
gen under18=(tage<18 & (ms~=1 & ms~=2))
gen under6=(tage<6 & (ms~=1 & ms~=2))
gen under5=(tage<5 & (ms~=1 & ms~=2))

gen parent_pnum = pnpt
gen HasParent = (parent_pnum~=999)
gen under24FTS_HasParent = (under24FTS==1 & parent_pnum~=999)
gen under19_HasParent = (under19==1 & parent_pnum~=999)
gen under18_HasParent = (under18==1 & parent_pnum~=999)
gen under6_HasParent = (under6==1 & parent_pnum~=999)
gen under5_HasParent = (under5==1 & parent_pnum~=999)
recode parent_pnum 999=.

gen PotentialDep = (under24FTS_HasParent==1 | under19_HasParent==1)

sort suid addid parent_pnum srefmon

by suid addid parent_pnum srefmon: egen KidCount = total(HasParent)
by suid addid parent_pnum srefmon: egen klt24m = total(under24FTS_HasParent)
by suid addid parent_pnum srefmon: egen klt19m = total(under19_HasParent)
by suid addid parent_pnum srefmon: egen klt18m = total(under18_HasParent)
by suid addid parent_pnum srefmon: egen klt6m = total(under6_HasParent)
by suid addid parent_pnum srefmon: egen klt5m = total(under5_HasParent)
by suid addid parent_pnum srefmon: egen DepCount = total(PotentialDep)

keep suid addid parent_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
drop if parent_pnum==.
duplicates drop 
rename parent_pnum pnum
count
sort suid addid pnum swave srefmon
save KidCount1993_w7, replace
** NOTE: So far this only incorporates the ONE parent listed in pnpt.  
** The next step is to make sure that a mom's count includes the kids
** listing her husband as parent, and that a dad's count includes the kids
** listing his wife as parent.


** Now rename variables to facilitate matching up spouse info
rename pnum pnsp 
rename KidCount sp_KidCount
rename klt24m sp_klt24m
rename klt19m sp_klt19m
rename klt18m sp_klt18m
rename klt6m sp_klt6m
rename klt5m sp_klt5m
rename DepCount sp_DepCount
count
sort suid addid pnsp swave srefmon
save SpouseKidCount1993_w7, replace
clear



************
** Now merge both own kid count and spouse's kid count back into main data.
quietly infile using sip93w7.dct, using(sipp93w7.dat)
destring pnum, replace
rename wave swave
rename refmth srefmon

gen rp_pnum = h5ref

keep suid entry pnum addid swave srefmon pnsp rp_pnum rrpu


** Merge in own kid count
sort suid addid pnum swave srefmon
merge 1:1 suid addid pnum swave srefmon using KidCount1993_w7
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort suid addid pnsp swave srefmon

merge m:1 suid addid pnsp swave srefmon using SpouseKidCount1993_w7, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge
sort suid addid pnum swave srefmon
save KidCount1993_w7, replace



egen Mod_KidCount = rowtotal(KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(klt18m sp_klt18m )
egen Mod_klt6m = rowtotal(klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(DepCount sp_DepCount)

sort suid entry pnum swave srefmon
save KidCount1993_w7, replace
clear




****************************************
** 1996
****************************************




***********
** Wave 3
***********

quietly infile using sip96l3.dct, using(sipp96l3.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

recode mom_pnum 9999=.

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)


sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount1996_w3, replace
clear


***********
** Wave 6
***********

quietly infile using sip96l6.dct, using(sipp96l6.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)


sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount1996_w6, replace
clear






***********
** Wave 9
***********

quietly infile using sip96l9.dct, using(sipp96l9.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount1996_w9, replace
clear





***********
** Wave 12
***********

quietly infile using sip96l12.dct, using(sipp96l12.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount1996_w12, replace
clear




**************
** 2001
**************


***********
** Wave 3
***********

quietly infile using sip01w3.dct, using(sipp01w3.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)


sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
saveKidCount2001_w3, replace
clear



***********
** Wave 6
***********

quietly infile using sip01w6.dct, using(sipp01w6.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2001_w6, replace
clear



***********
** Wave 9
***********

quietly infile using sip01w9.dct, using(sipp01w9.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m =total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2001_w9, replace
clear




**************
** 2004
**************


***********
** Wave 3
***********

quietly infile using sip04w3.dct, using(l04puw3.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)


sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2004_w3, replace
clear




***********
** Wave 6
***********

quietly infile using sip04w6.dct, using(l04puw6.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2004_w6, replace
clear



**************
** 2008
**************


***********
** Wave 4
***********

quietly infile using sippl08puw4.dct, using(l08puw4.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2008_w4, replace
clear




***********
** Wave 7
***********

quietly infile using sippl08puw7.dct, using(l08puw7.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS=(tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m = total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2008_w7, replace
clear




***********
** Wave 10
***********

quietly infile using sippl08puw10.dct, using(l08puw10.dat)
destring epppnum, replace


gen rp_pnum = ehrefper

keep ssuid shhadid epppnum swave srefmon tage epnmom epndad epnspous rp_pnum errp renroll ems

gen under24FTS = (tage<24 & renroll==1 & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))

save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=9999)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=9999)
gen under19_HasMom = (under19==1 & mom_pnum~=9999)
gen under18_HasMom = (under18==1 & mom_pnum~=9999)
gen under6_HasMom = (under6==1 & mom_pnum~=9999)
gen under5_HasMom = (under5==1 & mom_pnum~=9999)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)

recode mom_pnum 9999=.

sort ssuid mom_pnum srefmon

by ssuid mom_pnum srefmon: egen KidCount = total(HasMom)
by ssuid mom_pnum srefmon: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum srefmon: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum srefmon: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum srefmon: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum srefmon: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum srefmon: egen DepCount = total(PotentialDep)

** Make sure that ignoring shhadid isn't messing up your KidCount variable 
sort ssuid shhadid mom_pnum srefmon
by ssuid shhadid mom_pnum srefmon: egen KidCount2 = total(HasMom)
assert KidCount==KidCount2


** Save counts of kids for moms
keep ssuid mom_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum epppnum
sort ssuid epppnum swave srefmon
save MomKidCount_temp, replace

** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid epppnum swave srefmon
save OneParKidCount_temp, replace

rename epppnum epnspous 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspous swave srefmon
save SpouseKidCount_temp, replace


clear

** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=9999)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=9999)
gen under19_HasDad = (under19==1 & dad_pnum~=9999)
gen under18_HasDad = (under18==1 & dad_pnum~=9999)
gen under6_HasDad = (under6==1 & dad_pnum~=9999)
gen under5_HasDad = (under5==1 & dad_pnum~=9999)
recode dad_pnum 9999=.

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum srefmon

by ssuid dad_pnum srefmon: egen KidCount = total(HasDad)
by ssuid dad_pnum srefmon: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum srefmon: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum srefmon: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum srefmon: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum srefmon: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum srefmon: egen DepCount = total(PotentialDep)

keep ssuid dad_pnum swave srefmon KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum epppnum
sort ssuid epppnum swave srefmon
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid epppnum swave srefmon
merge ssuid epppnum swave srefmon using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspous swave srefmon 
merge ssuid epnspous swave srefmon using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge



egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid epppnum swave srefmon KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2008_w10, replace
clear




********************************************************************************
********************************************************************************
** 2014
********************************************************************************
********************************************************************************

**************
** Wave 1
**************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w1.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1, replace
clear

use ssuid monthcode pnum esex using pu2014w1.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt rfamref epnspouse using pu2014w1.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1


gen rp_pnum = rfamref


gen under24FTS = (tage<24 & (renroll==1 | eedftpt==1)  & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))



save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=.)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=.)
gen under19_HasMom = (under19==1 & mom_pnum~=.)
gen under18_HasMom = (under18==1 & mom_pnum~=.)
gen under6_HasMom = (under6==1 & mom_pnum~=.)
gen under5_HasMom = (under5==1 & mom_pnum~=.)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


sort ssuid mom_pnum monthcode

by ssuid mom_pnum monthcode: egen KidCount = total(HasMom)
by ssuid mom_pnum monthcode: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum monthcode: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum monthcode: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum monthcode: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum monthcode: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum monthcode: egen DepCount = total(PotentialDep)


** Save counts of kids for moms
keep ssuid mom_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum pnum
sort ssuid pnum monthcode
save MomKidCount_temp, replace


** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid pnum monthcode
save OneParKidCount_temp, replace

rename pnum epnspouse 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspouse monthcode
save SpouseKidCount_temp, replace


clear


** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=.)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=.)
gen under19_HasDad = (under19==1 & dad_pnum~=.)
gen under18_HasDad = (under18==1 & dad_pnum~=.)
gen under6_HasDad = (under6==1 & dad_pnum~=.)
gen under5_HasDad = (under5==1 & dad_pnum~=.)

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum monthcode

by ssuid dad_pnum monthcode: egen KidCount = total(HasDad)
by ssuid dad_pnum monthcode: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum monthcode: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum monthcode: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum monthcode: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum monthcode: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum monthcode: egen DepCount = total(PotentialDep)


keep ssuid dad_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum pnum
sort ssuid pnum monthcode
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid pnum monthcode
merge ssuid pnum monthcode using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid pnum monthcode
merge ssuid pnum monthcode using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid pnum monthcode
merge ssuid pnum monthcode using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspouse monthcode
merge ssuid epnspouse monthcode using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge


egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid pnum monthcode KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2014_w1, replace
clear




*****************
** 2014, Wave 2
*****************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w2.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w2, replace
clear

use ssuid monthcode pnum esex using pu2014w2.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w2, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt rfamref epnspouse using pu2014w2.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w2
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w2
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1


gen rp_pnum = rfamref


gen under24FTS = (tage<24 & (renroll==1 | eedftpt==1)  & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))



save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=.)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=.)
gen under19_HasMom = (under19==1 & mom_pnum~=.)
gen under18_HasMom = (under18==1 & mom_pnum~=.)
gen under6_HasMom = (under6==1 & mom_pnum~=.)
gen under5_HasMom = (under5==1 & mom_pnum~=.)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


sort ssuid mom_pnum monthcode

by ssuid mom_pnum monthcode: egen KidCount = total(HasMom)
by ssuid mom_pnum monthcode: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum monthcode: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum monthcode: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum monthcode: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum monthcode: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum monthcode: egen DepCount = total(PotentialDep)


** Save counts of kids for moms
keep ssuid mom_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum pnum
sort ssuid pnum monthcode
save MomKidCount_temp, replace


** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid pnum monthcode
save OneParKidCount_temp, replace

rename pnum epnspouse 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspouse monthcode
save SpouseKidCount_temp, replace


clear


** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=.)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=.)
gen under19_HasDad = (under19==1 & dad_pnum~=.)
gen under18_HasDad = (under18==1 & dad_pnum~=.)
gen under6_HasDad = (under6==1 & dad_pnum~=.)
gen under5_HasDad = (under5==1 & dad_pnum~=.)

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum monthcode

by ssuid dad_pnum monthcode: egen KidCount = total(HasDad)
by ssuid dad_pnum monthcode: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum monthcode: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum monthcode: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum monthcode: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum monthcode: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum monthcode: egen DepCount = total(PotentialDep)


keep ssuid dad_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum pnum
sort ssuid pnum monthcode
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid pnum monthcode
merge ssuid pnum monthcode using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid pnum monthcode
merge ssuid pnum monthcode using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid pnum monthcode
merge ssuid pnum monthcode using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspouse monthcode
merge ssuid epnspouse monthcode using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge


egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid pnum monthcode KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2014_w2, replace
clear



*****************
** 2014, Wave 3
*****************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w3_13.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w3, replace
clear

use ssuid monthcode pnum esex using pu2014w3_13.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w3, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt rfamref epnspouse using pu2014w3_13.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w3
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w3
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1


gen rp_pnum = rfamref


gen under24FTS = (tage<24 & (renroll==1 | eedftpt==1)  & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))



save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=.)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=.)
gen under19_HasMom = (under19==1 & mom_pnum~=.)
gen under18_HasMom = (under18==1 & mom_pnum~=.)
gen under6_HasMom = (under6==1 & mom_pnum~=.)
gen under5_HasMom = (under5==1 & mom_pnum~=.)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


sort ssuid mom_pnum monthcode

by ssuid mom_pnum monthcode: egen KidCount = total(HasMom)
by ssuid mom_pnum monthcode: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum monthcode: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum monthcode: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum monthcode: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum monthcode: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum monthcode: egen DepCount = total(PotentialDep)


** Save counts of kids for moms
keep ssuid mom_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum pnum
sort ssuid pnum monthcode
save MomKidCount_temp, replace


** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid pnum monthcode
save OneParKidCount_temp, replace

rename pnum epnspouse 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspouse monthcode
save SpouseKidCount_temp, replace


clear


** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=.)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=.)
gen under19_HasDad = (under19==1 & dad_pnum~=.)
gen under18_HasDad = (under18==1 & dad_pnum~=.)
gen under6_HasDad = (under6==1 & dad_pnum~=.)
gen under5_HasDad = (under5==1 & dad_pnum~=.)

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum monthcode

by ssuid dad_pnum monthcode: egen KidCount = total(HasDad)
by ssuid dad_pnum monthcode: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum monthcode: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum monthcode: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum monthcode: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum monthcode: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum monthcode: egen DepCount = total(PotentialDep)


keep ssuid dad_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum pnum
sort ssuid pnum monthcode
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid pnum monthcode
merge ssuid pnum monthcode using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid pnum monthcode
merge ssuid pnum monthcode using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid pnum monthcode
merge ssuid pnum monthcode using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspouse monthcode
merge ssuid epnspouse monthcode using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge


egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid pnum monthcode KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2014_w3, replace
clear




*****************
** 2014, Wave 4
*****************


** First just extract identifying variables and sex, to help in figuring out
** sex of parent 1 and sex of parent 2
use ssuid monthcode pnum esex using pu2014w4.dta
rename pnum epnpar1
rename esex sex_par1
save Sex_Par1_2014_w4, replace
clear

use ssuid monthcode pnum esex using pu2014w4.dta
rename pnum epnpar2
rename esex sex_par2
save Sex_Par2_2014_w4, replace
clear

**********************************************************************************************
** Now extract variables for main part of this family data assembly


use ssuid pnum spanel swave monthcode tage ems epnpar1 epnpar2 esex renroll eedftpt rfamref epnspouse using pu2014w4.dta

** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar1 using Sex_Par1_2014_w4
keep if _merge~=2
drop _merge


** Merge in info on sex of each parent
merge m:1 ssuid monthcode epnpar2 using Sex_Par2_2014_w4
keep if _merge~=2
drop _merge

** Make epnmom, setting it equal to epnpar1 if parent 1 is female, replacing it
** with epnpar2 if (and only if) epnpar1 is male.  Repeat same idea to make epndad
gen epnmom = .
replace epnmom = epnpar1 if sex_par1==2
replace epnmom = epnpar2 if sex_par1==1 & sex_par2==2

gen epndad = .
replace epndad = epnpar1 if sex_par1==1
replace epndad = epnpar2 if sex_par1==2 & sex_par2==1


gen rp_pnum = rfamref


gen under24FTS = (tage<24 & (renroll==1 | eedftpt==1)  & (ems~=1 & ems~=2))
gen under19=(tage<19 & (ems~=1 & ems~=2))
gen under18=(tage<18 & (ems~=1 & ems~=2))
gen under6=(tage<6 & (ems~=1 & ems~=2))
gen under5=(tage<5 & (ems~=1 & ems~=2))



save temp, replace


** Create variables for moms

gen mom_pnum = epnmom
gen HasMom = (mom_pnum~=.)
gen under24FTS_HasMom = (under24FTS==1 & mom_pnum~=.)
gen under19_HasMom = (under19==1 & mom_pnum~=.)
gen under18_HasMom = (under18==1 & mom_pnum~=.)
gen under6_HasMom = (under6==1 & mom_pnum~=.)
gen under5_HasMom = (under5==1 & mom_pnum~=.)

gen PotentialDep = (under24FTS_HasMom==1 | under19_HasMom==1)


sort ssuid mom_pnum monthcode

by ssuid mom_pnum monthcode: egen KidCount = total(HasMom)
by ssuid mom_pnum monthcode: egen klt24m =total(under24FTS_HasMom)
by ssuid mom_pnum monthcode: egen klt19m = total(under19_HasMom)
by ssuid mom_pnum monthcode: egen klt18m = total(under18_HasMom)
by ssuid mom_pnum monthcode: egen klt6m = total(under6_HasMom)
by ssuid mom_pnum monthcode: egen klt5m = total(under5_HasMom)
by ssuid mom_pnum monthcode: egen DepCount = total(PotentialDep)


** Save counts of kids for moms
keep ssuid mom_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if mom_pnum==.
rename mom_pnum pnum
sort ssuid pnum monthcode
save MomKidCount_temp, replace


** Now, treat the mom variables as if they are the variables for the one identified
** parent, a la the 1993 panel.  Save a version that will be merged with identified spouses


rename KidCount OnePar_KidCount
rename klt24m OnePar_klt24m
rename klt19m OnePar_klt19m
rename klt18m OnePar_klt18m
rename klt6m OnePar_klt6m
rename klt5m OnePar_klt5m
rename DepCount OnePar_DepCount
sort ssuid pnum monthcode
save OneParKidCount_temp, replace

rename pnum epnspouse 
rename OnePar_KidCount sp_KidCount
rename OnePar_klt24m sp_klt24m
rename OnePar_klt19m sp_klt19m
rename OnePar_klt18m sp_klt18m
rename OnePar_klt6m sp_klt6m
rename OnePar_klt5m sp_klt5m
rename OnePar_DepCount sp_DepCount
sort ssuid epnspouse monthcode
save SpouseKidCount_temp, replace


clear


** Now read basic file back in and create variables for dads
use temp

gen dad_pnum = epndad
gen HasDad = (dad_pnum~=.)
gen under24FTS_HasDad = (under24FTS==1 & dad_pnum~=.)
gen under19_HasDad = (under19==1 & dad_pnum~=.)
gen under18_HasDad = (under18==1 & dad_pnum~=.)
gen under6_HasDad = (under6==1 & dad_pnum~=.)
gen under5_HasDad = (under5==1 & dad_pnum~=.)

gen PotentialDep = (under24FTS_HasDad==1 | under19_HasDad==1)

sort ssuid dad_pnum monthcode

by ssuid dad_pnum monthcode: egen KidCount = total(HasDad)
by ssuid dad_pnum monthcode: egen klt24m = total(under24FTS_HasDad)
by ssuid dad_pnum monthcode: egen klt19m = total(under19_HasDad)
by ssuid dad_pnum monthcode: egen klt18m = total(under18_HasDad)
by ssuid dad_pnum monthcode: egen klt6m = total(under6_HasDad)
by ssuid dad_pnum monthcode: egen klt5m = total(under5_HasDad)
by ssuid dad_pnum monthcode: egen DepCount = total(PotentialDep)


keep ssuid dad_pnum monthcode KidCount klt24m klt19m klt18m klt6m klt5m DepCount
duplicates drop 
drop if dad_pnum==.
rename dad_pnum pnum
sort ssuid pnum monthcode
save DadKidCount_temp, replace
clear


*********************************************************************
** Now, merge parent identifiers back into full dataset

use temp

sort ssuid pnum monthcode
merge ssuid pnum monthcode using MomKidCount_temp
assert _merge~=2
rename _merge mom_merge

sort ssuid pnum monthcode
merge ssuid pnum monthcode using DadKidCount_temp, update
assert _merge~=2
assert mom_merge~=3 if _merge==3
assert _merge~=3 if mom_merge==3
drop _merge mom_merge


** For the 1993 comparison, merge in the "if only one parent is identified" kid count
sort ssuid pnum monthcode
merge ssuid pnum monthcode using OneParKidCount_temp, update
assert _merge~=2
drop _merge

** Merge in spouse's kid count
sort ssuid epnspouse monthcode
merge ssuid epnspouse monthcode using SpouseKidCount_temp, update
tab _merge
** Note: There will be _merge==2 cases.  These correspond to unmarried people.  You have already recorded
** their kid info in their own KidCount variables, and there is not a spouse to match them up with.
drop if _merge==2
drop _merge


egen Mod_KidCount = rowtotal(OnePar_KidCount sp_KidCount)
egen Mod_klt24m = rowtotal(OnePar_klt24m sp_klt24m)
egen Mod_klt19m = rowtotal(OnePar_klt19m sp_klt19m )
egen Mod_klt18m = rowtotal(OnePar_klt18m sp_klt18m)
egen Mod_klt6m = rowtotal(OnePar_klt6m sp_klt6m )
egen Mod_klt5m = rowtotal(OnePar_klt5m sp_klt5m )
egen Mod_DepCount = rowtotal(OnePar_DepCount sp_DepCount)

keep ssuid pnum monthcode KidCount klt24m klt19m klt18m klt5m klt6m DepCount Mod_KidCount Mod_klt24m Mod_klt19m Mod_klt18m Mod_klt6m Mod_klt5m Mod_DepCount
save KidCount2014_w4, replace
clear
